using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Domain.Entities;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using JGSY.CMS.LowCode.Platform.Infrastructure.CmsDbContext;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Repositories
{
    /// <summary>
    /// 登录历史仓储实现
    /// 提供登录历史记录的数据访问功能实现，使用内存存储
    /// </summary>
    public class LoginHistoryRepository : ILoginHistoryRepository
    {
        private readonly LowCodeDbContext _context;
        /// <summary>
        /// 登录历史记录集合
        /// </summary>
        private readonly List<LoginHistory> _list = new();
        
        public LoginHistoryRepository(LowCodeDbContext context)
        {
            _context = context;
        }
        
        /// <summary>
        /// 获取所有登录历史记录
        /// </summary>
        /// <returns>登录历史记录列表</returns>
        public async Task<IEnumerable<LoginHistory>> GetAllAsync()
        {
            await Task.CompletedTask;
            return _list;
        }
        
        /// <summary>
        /// 根据ID获取登录历史记录
        /// </summary>
        /// <param name="id">登录历史记录标识符</param>
        /// <returns>登录历史记录实体，如果不存在则返回null</returns>
        public async Task<LoginHistory?> GetByIdAsync(long id)
        {
            await Task.CompletedTask;
            return _list.FirstOrDefault(e => e.Id == id);
        }
        
        /// <summary>
        /// 添加新的登录历史记录
        /// </summary>
        /// <param name="entity">登录历史记录实体</param>
        /// <returns>添加操作的异步任务</returns>
        public async Task AddAsync(LoginHistory entity)
        {
            await Task.CompletedTask;
            _list.Add(entity);
        }
        
        /// <summary>
        /// 更新登录历史记录
        /// </summary>
        /// <param name="id">登录历史记录标识符</param>
        /// <param name="entity">更新后的登录历史记录实体</param>
        /// <returns>更新操作的异步任务</returns>
        public async Task UpdateAsync(long id, LoginHistory entity)
        {
            await Task.CompletedTask;
            var idx = _list.FindIndex(e => e.Id == id);
            if (idx >= 0) _list[idx] = entity;
        }
        
        /// <summary>
        /// 删除登录历史记录
        /// </summary>
        /// <param name="id">登录历史记录标识符</param>
        /// <returns>删除操作的异步任务</returns>
        public async Task DeleteAsync(long id)
        {
            await Task.CompletedTask;
            _list.RemoveAll(e => e.Id == id);
        }
    }
}
